package service;

import domain.House;

public class HouseService {
    private House[] houses;
    int num=0;
    int idCounter=1;
    //构造器
    public HouseService(int size) {
        houses=new House[size];
    }
    //find方法
    public House find(int id) {
        for(int i=0;i<houses.length;i++) {
            if(houses[i].getId()==id) {
                return houses[i];
            }
        }
        return null;
    }
    //delete方法
    public boolean del(int delId){
        int index=-1;
        for(int i=0;i<num;i++){
            if(houses[i].getId()==delId){
                index=i;
            }
        }
        if(index==-1){
            return false;
        }else{
            for(int i=index;i<num-1;i++){
                houses[i]=houses[i+1];
            }
        }
        houses[--num]=null;
        return true;
    }
    //add方法
    public boolean add(House newHouse) {
        if(num==houses.length) {
            System.out.println("数组已满，不能再添加了...");
            return false;
        }
        houses[num++]=newHouse;
        newHouse.setId(idCounter++);
        return true;
    }

    //list方法
    public House[] list(){
        return houses;
    }


}
